/*
* $Id: HousingDialog.cpp 143 2010-09-09 19:55:18Z volok-aleksej@yandex.ru $
*
* Copyright 2009 by Volokitin Alexey
* All rights reserved
*
*/
#include "Common/include.h"
#include "HousingDialog.h"
#include "Database/Tables/HousingsTable.h"
#include "Database/Tables/TypesTable.h"

CHousingDialog::CHousingDialog()
: CDialog(IDD_NEW_HOUSE, dynamic_cast<CWnd*>(IVisualManager::GetVisualManager()->
		  GetVisualComponent(ILanguageManager::GetLanguageManager()->GetString(JOURNAL_CAPTION_ID))->GetComponentFrame())),
	m_nFrames(0), m_nIdHousing(0), m_nIdFamily(0), m_nTypeHousing(0), m_bUpdate(false){
		GetTypes();
}
CHousingDialog::CHousingDialog(int nId, std::tstring strName, int nType, int nFrames, int nIdFamily)
: CDialog(IDD_NEW_HOUSE, dynamic_cast<CWnd*>(IVisualManager::GetVisualManager()->
		  GetVisualComponent(ILanguageManager::GetLanguageManager()->GetString(JOURNAL_CAPTION_ID))->GetComponentFrame())),
	m_nIdHousing(nId), m_strName(strName), m_nTypeHousing(nType), m_nFrames(nFrames), m_nIdFamily(nIdFamily), m_bUpdate(true){
		GetTypes();
}
CHousingDialog::~CHousingDialog(){}

void CHousingDialog::GetTypes(){
	IDatabase* pDb = IDatabaseManager::GetDatabaseManager()->OpenDatabase(JOURNAL_DATABASE_NAME);
	CTypeHousingsTable tableH;
	IResult* result = tableH.GetValue(pDb);
	for(int i = 0; i < (int)result->GetSizeResult(); i++){
		CTypeHousingsTable::content* pContent = (CTypeHousingsTable::content*)(*result)[i];
		if(pContent->s_strName.empty() && pContent->s_nId < 7){
			m_aTypesHousing.insert(std::make_pair(pContent->s_nId,
				ILanguageManager::GetLanguageManager()->GetString(EIGHT_FRAME_HOUSING_ID + pContent->s_nId)));
		}else
			m_aTypesHousing.insert(std::make_pair(pContent->s_nId, pContent->s_strName));
	}
	result->Free();
	CTypeFrameTable tableF;
	result = tableF.GetValue(pDb);
	for(int i = 0; i < (int)result->GetSizeResult(); i++){
		CTypeFrameTable::content* pContent = (CTypeFrameTable::content*)(*result)[i];
		if(pContent->s_strName.empty() && pContent->s_nId < 7){
			m_aTypesFrame.insert(std::make_pair(pContent->s_nId,
				ILanguageManager::GetLanguageManager()->GetString(S300_ID + pContent->s_nId)));
		}else
			m_aTypesFrame.insert(std::make_pair(pContent->s_nId, pContent->s_strName));
	}
	result->Free();
	IDatabaseManager::GetDatabaseManager()->CloseDatabase(pDb);
}

BOOL CHousingDialog::OnInitDialog(){
	BOOL bRes = __super::OnInitDialog();
	SetWindowText(ILanguageManager::GetLanguageManager()->GetString(ADD_HOUSING_ID).c_str());
	SetDlgItemText(IDC_NAME_HOUSE, ILanguageManager::GetLanguageManager()->GetString(HOUSING_NAME_ID).c_str());
	SetDlgItemText(IDC_TYPE_HOUSE, ILanguageManager::GetLanguageManager()->GetString(HOUSING_TYPE_ID).c_str());
	SetDlgItemText(IDC_TYPE_FRAME, ILanguageManager::GetLanguageManager()->GetString(HOUSING_FRAME_ID).c_str());
	if(m_bUpdate)
		SetDlgItemText(IDOK, ILanguageManager::GetLanguageManager()->GetString(CHANGE_ID).c_str());
	else
		SetDlgItemText(IDOK, ILanguageManager::GetLanguageManager()->GetString(ADD_ID).c_str());
	SetDlgItemText(IDCANCEL, ILanguageManager::GetLanguageManager()->GetString(CANCEL_ID).c_str());

	SetDlgItemText(IDC_NAMEH_EDIT, m_strName.c_str());

	for(std::map<int, std::tstring>::iterator it = m_aTypesHousing.begin(); it != m_aTypesHousing.end(); it++){
		int cur = m_typeHousing.AddString(it->second.c_str());
		m_typeHousing.SetItemData(cur, it->first);
		if(it->first == m_nTypeHousing)
			m_typeHousing.SetCurSel(cur);
	}

	for(std::map<int, std::tstring>::iterator it = m_aTypesFrame.begin(); it != m_aTypesFrame.end(); it++){
		int cur = m_typeFrame.AddString(it->second.c_str());
		m_typeFrame.SetItemData(cur, it->first);
		if(it->first == m_nTypeHousing)
			m_typeFrame.SetCurSel(cur);
	}
	return bRes;
}

void CHousingDialog::DoDataExchange(CDataExchange* pDX){
	DDX_Control(pDX, IDC_TYPEH_EDIT, m_typeHousing);
	DDX_Control(pDX, IDC_TYPEF_EDIT, m_typeFrame);
	__super::DoDataExchange(pDX);
}

void CHousingDialog::OnOK(){
	CString strValue;
	GetDlgItemText(IDC_NAMEH_EDIT, strValue);
	m_strName = strValue.GetString();
	m_nTypeHousing = (int)m_typeHousing.GetItemData(m_typeHousing.GetCurSel());
	m_nFrames = (int)m_typeFrame.GetItemData(m_typeFrame.GetCurSel());
	IDatabase* pDb = IDatabaseManager::GetDatabaseManager()->OpenDatabase(JOURNAL_DATABASE_NAME);
	CHousingsTable table;
	table.m_content.s_strName = m_strName;
	table.m_content.s_nType = m_nTypeHousing;
	table.m_content.s_nFrames = m_nFrames;
	table.m_content.s_nIdFamily = m_nIdFamily;
	if(m_bUpdate){
		table.m_content.s_bDelete = false;
		table.m_content.s_nId = m_nIdHousing;
		table.UpdateValue(pDb);
	}else{
		table.SetValue(pDb);
		m_nIdHousing = table.m_content.s_nId;
	}
	IDatabaseManager::GetDatabaseManager()->CloseDatabase(pDb);
	EndDialog(IDOK);
}

int CHousingDialog::GetIdHousing(){
	return m_nIdHousing;
}

std::tstring CHousingDialog::GetNameHousing(){
	return m_strName;
}

int CHousingDialog::GetTypeHousing(){
	return m_nTypeHousing;
}

int CHousingDialog::GetTypeFrames(){
	return m_nFrames;
}
